﻿using System.Collections.ObjectModel;
using System.IO;
using System.Windows.Input;
using MediaMover.Common;
using MediaMover.Data;
using MediaMover.MVVM.Commands;
using Ninject;

namespace MediaMover.MVVM.ViewModels
{
    public class OptionsViewModel : WorkspaceViewModel
    {
        private Access _access;
        private Settings _settings;
        public OptionsViewModel(IKernel kernel)
        {
            _access = kernel.Get<Access>();
            DisplayName = "Options";
            _settings = _access.SettingsRepository.GetById(1);
        }

        #region Commands
        private ICommand _saveCommand;
        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new RelayCommand(param => Save(), param => true);
                }
                return _saveCommand;
            }
            set { _saveCommand = value; }
        }
        
        private void Save()
        {
            _access.SettingsRepository.Update(_settings);
        }

        private ICommand _addSearchLocationCommand;
        public ICommand AddSearchLocationCommand
        {
            get
            {
                if (_addSearchLocationCommand == null)
                {
                    _addSearchLocationCommand = new RelayCommand(param => AddNewSearchLocation(),
                                                          param => CanAddNewSearchLocation());
                }
                return _addSearchLocationCommand;
            }
            set { _addSearchLocationCommand = value; }
        }

        private void AddNewSearchLocation()
        {
            SearchLocations.Add(SearchLocationToAdd);
        }

        private bool CanAddNewSearchLocation()
        {
            return string.IsNullOrEmpty(_searchLocationToAdd) == false && Directory.Exists(_searchLocationToAdd) && SearchLocations.Contains(SearchLocationToAdd) == false;
        }

        private ICommand _removeSelectedSearchLocationCommand;
        public ICommand RemoveSelectedSearchLocationCommand
        {
            get
            {
                if (_removeSelectedSearchLocationCommand == null)
                {
                    _removeSelectedSearchLocationCommand = new RelayCommand(param => RemoveSelectedSearchLocation(),
                                                             param => CanRemoveSelectedSearchLocation());
                }
                return _removeSelectedSearchLocationCommand;
            }
            set { _removeSelectedSearchLocationCommand = value; }
        }

        private void RemoveSelectedSearchLocation()
        {
            SearchLocations.Remove(SelectedSearchLocation);
        }

        private bool CanRemoveSelectedSearchLocation()
        {
            return string.IsNullOrEmpty(SelectedSearchLocation) == false && SearchLocations.Contains(SelectedSearchLocation);
        }
        #endregion
        #region Properties
        public string TvBaseLocation
        {
            get { return _settings.TvBaseLocation; }
            set { _settings.TvBaseLocation = value; }
        }

        public string MovieBaseLocation
        {
            get { return _settings.MovieBaseLocation; }
            set { _settings.MovieBaseLocation = value; }
        }

        public ObservableCollection<string> SearchLocations
        {
            get
            {
                if (_settings.SearchLocations == null)
                    _settings.SearchLocations = new ObservableCollection<string>();
                return _settings.SearchLocations as ObservableCollection<string>;
            }
            set { _settings.SearchLocations = value; }
        }

        public string SelectedSearchLocation { get; set; }
        private string _searchLocationToAdd;
        public string SearchLocationToAdd
        {
            get { return _searchLocationToAdd; }
            set { _searchLocationToAdd = value; }
        }
        #endregion
    }
}
